/**
 * 
 */
package webapps.study.domain.repository;

import java.io.IOException;
import java.util.Properties;

/**
 * @author Kevin.Huang
 * @date 2007-9-26
 */
public final class DB4OConfig {

	private DB4OConfig() {
	}

	/**
	 * 配置文件名
	 */
	private static String configuration = "db4o.properties";

	private static Properties props = null;

	private static boolean changed = false;

	public static String getDataPath() {
		load();
		String path = props.getProperty("db4o.path");
		throwException(path);
		return path.trim();
	}

	/**
	 * @param path
	 */
	private static void throwException(String path) {
		if (path == null) {
			throw new RuntimeException("key is not founded in " + configuration);
		}
	}

	/**
	 * @throws IOException
	 * @throws IOException
	 * @throws MalformedURLException
	 */
	private static void load() {
		try {
			if (props == null || changed) {
				props = new Properties();
				props.load(Resources.getResourceAsStream(configuration));
				changed = false;
			}
		} catch (IOException e) {
			// XXX 记录异常
			throw new RuntimeException("configuration: " + configuration + " is not founded!");
		}
	}

	/**
	 * @param configuration
	 *            the configuration to set
	 */
	public static void setConfiguration(String configuration) {
		DB4OConfig.changed = true;
		DB4OConfig.configuration = configuration;
	}

}
